package _base0to1;

/**
 * 283. 移动零
 */
public class No283 {
    /**
     * 1. 暴力
     */
    public void moveZeroes1(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] != 0) {
                        nums[i] = nums[j];
                        nums[j] = 0;
                        break;
                    }
                }
            }
        }
    }

    /**
     * 2. 两次遍历
     */
    public void moveZeroes2(int[] nums) {
        int left = 0, right = 0;
        for (; right < nums.length; right++) {
            if (nums[right] != 0) {
                nums[left++] = nums[right];
            }
        }
        for (; left < nums.length; left++) {
            nums[left] = 0;
        }
    }

    /**
     * 3. 一次遍历
     */
    public void moveZeroes3(int[] nums) {
        int left = 0, right = 0;
        for (; right < nums.length; right++) {
            if (nums[right] != 0) {
                int temp = nums[left];
                nums[left++] = nums[right];
                nums[right] = temp;
            }
        }
    }
}
